/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.manage.cart;
import shop.core.common.bean.JSONResult;
import shop.core.common.dao.page.PagerModel;
import shop.core.util.KeyValueHelper;
import shop.core.Services;
import shop.core.util.MD5;
import shop.services.manage.cart.CartService;
import shop.services.manage.cart.bean.Cart;
import shop.core.BaseController;
import shop.core.RequestHolder;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**   
 * @类名称：CartAction      
 * @创建人：wzl
 * @创建时间：2018-05-03 上午09:51:33      
 * @版本号：1.0
 * @描述：     
 */
@Controller
@RequestMapping("/manage/cart/")
public class CartAction extends BaseController<Cart> {
    private static final Logger logger = LoggerFactory.getLogger(CartAction.class);
    @Autowired
    private CartService cartService;
    private static final String page_toList = "/manage/cart/cartList";
    public CartService getService() {
        return cartService;
    }

    private CartAction(){
        super.page_toList = page_toList;
        super.page_toAdd = null;
        super.page_toEdit = null;
    }

    /**
     * 插入车辆信息记录
     * @param request
     * @param cart
     * @return
     * @throws Exception
     */
    @Override
    @RequestMapping(value = "insertJson", method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertJson(HttpServletRequest request, Cart cart) throws Exception {
        JSONResult jsonResult=new JSONResult();
        Cart cartInfo=new Cart();
        cartInfo.setCartNo(cart.getCartNo());
        cartInfo=cartService.selectOne(cartInfo);
        if(cartInfo==null){
            cartService.insert(cart);
            cart=cartService.selectOne(cart);
            jsonResult.setData(cart.getCartNo());
            jsonResult.setMessage("插入成功！");
            jsonResult.setSuccess(true);
            return jsonResult;
        }else{
            jsonResult.setMessage("该条记录已存在，请勿重复添加！");
            jsonResult.setSuccess(false);
            return jsonResult;
        }
    }

    /**
     * 分页查询车辆信息
     *
     * @param request 请求
     * @param cart    对象参数
     * @return 分页数据模型
     */
    @RequestMapping("selectCartPage")
    @ResponseBody
    public PagerModel selectCartPage(HttpServletRequest request, Cart cart) {
        int offset = 0;
        int pageSize = 10;
        int start = Integer.parseInt(request.getParameter("start"));
        int length = Integer.parseInt(request.getParameter("length"));
        if (start > 0){
            offset = start;
        }
        if (length > 0){
            pageSize = length;
        }
        cart.setOffset(offset);
        cart.setPageSize(pageSize);
        PagerModel pager = cartService.selectCartPage(cart);
        pager.setRecordsTotal(pager.getTotal());
        pager.setRecordsFiltered(pager.getTotal());
        return pager;
    }
}
